<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>设备管理</title>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css"></link>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/xtheme-slate.css"></link>
<script src="../ext/adapter/ext/ext-base.js"></script>
<script src="../ext/ext-all.js"></script>
<script src="../ext/src/locale/ext-lang-zh_CN.js"></script>	
<script type="text/javascript">
Ext.onReady(function(){
	Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);      
	function checkUserSessionStatus(conn,response,options){      
	    if(response.getResponseHeader('sessionstatus')){      
	    	parent.location.href='/xdj/index.jsp';   
	    }      
	}  
	Ext.BLANK_IMAGE_URL='ext/resources/images/default/s.gif';
	var deviceForm ;
	var panel;
	var win;
	function showWindown(urlString) {
		deviceForm=new Ext.form.FormPanel({
			frame:true,
			height:400,
			buttonAlign:'left',
			
			labelAlign:'right',
			layout:'form',
			items:[{
						fieldLabel:'设备名称',
						xtype:'textfield',
						allowBlank:false,
						blankText:'名称不能为空'	,
						name:'deviceBaseInfo.deviceName'	
			},{
					layout:'column',
					items:[{
						layout:'form',
						items:[{
							fieldLabel:'设备编码',
							xtype:'textfield',
							width:200,
							name:'deviceBaseInfo.deviceCode'
						}]		
					},{
						layout:'form',
						items:[{
							fieldLabel:'KKS编码',
							xtype:'textfield',
							name:'deviceBaseInfo.deviceKKSCode'
						}]		
					}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					//columnWidth:.30,
					items:[{
						fieldLabel:'安装位置',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devBuildPlace'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'位置编码',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devPlaceCode'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					//columnWidth:.30,
					items:[{
						fieldLabel:'图纸编号',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devDrawingNo'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'设备规格',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devSpecification'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					//columnWidth:.30,
					items:[{
						fieldLabel:'设备型号',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.deviceModel'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'出厂日期',
						xtype : 'textfield',
						//format : 'y-m-d H:i:s',
						width:200,
						name:'deviceBaseInfo.devOutDate'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					//columnWidth:.30,
					items:[{
						fieldLabel:'出厂编号',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devOutCode'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'安装单位',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devBuildCompany'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					//columnWidth:.30,
					items:[{
						fieldLabel:'制造厂家',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devManuFactory'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'使用部门',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devUseDepart'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					//columnWidth:.30,
					items:[{
						fieldLabel:'投运日期',
						xtype:'textfield',
						//format:'y-m-d H:i:s',
						width:200,
						name:'deviceBaseInfo.devRunDate'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'设置状态',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devUseStatus'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
				//	columnWidth:.30,
					items:[{
						fieldLabel:'供应商',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devProvider'
					}]		
				},{
					layout:'form',
					items:[{
						fieldLabel:'添置日期',
						xtype:'textfield',
					//	format:'y-m-d H:i:s',
						width:200,
						name:'deviceBaseInfo.devAddDate'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					items:[{
						fieldLabel:'合同号',
						xtype:'textfield',
						width:200,
						name:'deviceBaseInfo.devContractNo'
					}]		
				}]
			},{
				layout:'column',
				items:[{
					layout:'form',
					items:[{
						fieldLabel:'添置说明',
						xtype:'textarea',
						width:200,
						height:50,
						grow:true, //可自动增长
						preventScrollbars:true , //不显示那个下拉条 必须与grow一起用
						name:'deviceBaseInfo.addRemark'
					}]
				},{
					layout:'form',
					itmes:[{
						fieldLabel:'设备说明',
						xtype:'textarea',
						width:200,
						height:10,
						grow:true, //可自动增长
						preventScrollbars:true , //不显示那个下拉条 必须与grow一起用
						name:'deviceBaseInfo.deviceRemark'
					}]
				}]	
			},{
				xtype:'hidden',
				//xtype:'textfield',
				name:'deviceBaseInfo.deviceInfoID'
			}],
			buttons:[{
		       text: "确认",
		       handler:function(){ 
		    	   if ( deviceForm.getForm().isValid() ){
		    		   deviceForm.form.submit({   
		        	waitMsg : '正在提交数据....',   
		            url:urlString,   
		            method:'post',   
		            success:function(deviceForm,action){  
		            	var result = Ext.util.JSON.decode(action.response.responseText);
		            	if(result.success==true) {
		            		//alert(deptForm.getForm().findField('dept.depName').getValue()) ;
		            		if(urlString=="device_save"){
		            			
			            		var nodex = new Ext.tree.TreeNode({
			            				id : result.id,  
			        					text : result.name,  
			        					cls : 'folder' 
			            		});
								thenode.appendChild(nodex);
								thenode.reload() ;
								
		            		}else{
		            			store.reload();
		            			
		            		}
		            		
							win.close() ;
							Ext.getBody().unmask();
		            		Ext.Msg.alert("提示","操作成功。") ;
		            	}else{
		            		Ext.Msg.alert("提示","操作失败。") ;
		            	}
		            }
		          }); 
		       }
		       }
		     },{
		            text: "取消",
		           	handler:function(){ 
		           		win.close() ;
		           		Ext.getBody().unmask();
		           	}
		            
		        }
		     ]
		});
	 win=new Ext.Window({
		//title:'登录',
		 width:700,
		 height:430,
		 items:[deviceForm],
		 xtype:'form',
		 listeners:{"beforedestroy":function(obj)
             { 
			 Ext.getBody().unmask();  
                // obj.show(); return false; 
              }
		}  
	});
	win.show();
	
	}
	
	
	//表格
	//var sm = new Ext.grid.CheckboxSelectionModel();//列的复选框
	var cm=new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer(),  //自动增加序列号
		//sm, //加到每列的前边
		//{header:'岗位编码',dataIndex:"stationCode",sortable:true}, //True表示为可在该列上进行排列
		{header:'设备名称',dataIndex:"deviceName",sortable:true},
		{header:'安装位置',dataIndex:"place"},
		{header:'安装单位',dataIndex:"department"},
		{header:'使用部门',dataIndex:"useDepartment"},
		{header:'ID',dataIndex:"deviceInfoID",hidden:true}
	]);//定义相应的列模型
	var store=new Ext.data.JsonStore({
		url: 'device_load',
		root: 'result',
		// idProperty:'id',
		totalProperty:'totalProperty',
	    fields: ["deviceName","place","department","useDepartment","deviceInfoID"]
	});//存储前两项的定义的东西，将其连接起来
	
	 panel = new Ext.grid.GridPanel({
		 region:'center',
		border:false,
		cm:cm,
		//sm:sm,
		store:store,
		//height:400,
		height :  document.body.clientHeight, //设置表格的高度 true为自动获取表格的大小
		renderTo:'grid' , //渲染容器
		enableColumnMove:false,//不可以使表格列移动
		enableColumnResize:false, //不可以改变列的宽度
		stripeRows:false, //True表示为显示行的分隔符
		loadMask:true,  //会有一个Ext.LoadMask的遮罩效果
		trackMouseOver : true,  //True表示为鼠标移动时高亮显示（默认为true）。
		enableDragDrop : true,
		viewConfig:{
			forceFit:true,//让没列自动填满表格，根据你设置的宽度按比例分配
			center: {autoScroll: true}
		},tbar:[{
			text:'查看',disabled:true,id:'cToolViewDeviceTreeNode',
			icon:'../image/public/user_select.png',
			handler:function(){
				Ext.getBody().mask();
				var record=panel.getSelectionModel().getSelected();
				if(record ==null){
					alert("请选择要查看的设备");
					Ext.getBody().unmask();
				}else{
					var deviceId=record.get("deviceInfoID");
					alert(deviceId);
					Ext.Ajax.request({
						url:'device_get',
						params:{
							deviceInfoID:deviceId			
						},
						success: function(response){
							var result = Ext.util.JSON.decode(response.responseText);
							alert(result.deviceName+"===");
							if(result.success==true) {
								showWindown("device_get");
								deviceForm.setDisabled(true);
								deviceForm.getForm().setValues({
									'deviceBaseInfo.deviceName':result.deviceName,
									'deviceBaseInfo.deviceCode':result.deviceCode,
									'deviceBaseInfo.deviceKKSCode':result.deviceKKSCode,
									'deviceBaseInfo.devBuildPlace':result.devBuildPlace,
									'deviceBaseInfo.devPlaceCode':result.devPlaceCode,
									'deviceBaseInfo.devDrawingNo':result.devDrawingNo,
									'deviceBaseInfo.devSpecification':result.devSpecification,
									'deviceBaseInfo.deviceModel':result.deviceModel,
									'deviceBaseInfo.devOutDate':result.devOutDate,
									'deviceBaseInfo.devOutCode':result.devOutCode,
									'deviceBaseInfo.devBuildCompany':result.devBuildCompany,
									'deviceBaseInfo.devManuFactory':result.devManuFactory,
									'deviceBaseInfo.devUseDepart':result.devUseDepart,
									'deviceBaseInfo.devRunDate':result.devRunDate,
									'deviceBaseInfo.devUseStatus':result.devUseStatus,
									'deviceBaseInfo.devProvider':result.devProvider,
									'deviceBaseInfo.devAddDate':result.devAddDate,
									'deviceBaseInfo.devContractNo':result.devContractNo,
									'deviceBaseInfo.addRemark':result.addRemark,
									'deviceBaseInfo.deviceRemark':result.deviceRemark
									
								});
							}
						}
					});
				}
				
				
				
			}
		},{
			text: "删除",disabled:true,id:'cToolDeleteDeviceTreeNode',
			icon:'../image/public/user_delete.png',
        	handler:function(){
        		Ext.MessageBox.confirm("提示","您确定要删除这些信息吗?", function(button,text){   
        			if(button=='yes'){
        				var record=panel.getSelectionModel().getSelected();
        				if(record ==null){
        					alert("请选择要删除的设备");
        					Ext.getBody().unmask();
        				}else{
        					 var deviceID=record.get("deviceInfoID");
                       		 alert(deviceID);
               				Ext.Ajax.request({
               					url:'device_delete',
               					params:{
               						deviceInfoID:deviceID						
               					},
               					success:function(response){
               						var result = Ext.util.JSON.decode(response.responseText);
               						if(result.success==true){
               							store.reload() ;
               						}else{
               							Ext.Msg.alert("提示","操作失败，请重试。") ;
               						}
               					}
               				});
        				}
               		
        			}
        			}); 
			}
		},{
			text: "修改",disabled:true,id:'cSaveDeviceBox',
			icon:'../image/public/user_edit.png',
			handler:function(){
				Ext.MessageBox.confirm("提示","您确定要修改这些信息吗?", function(button,text){   
					if(button=='yes'){
						Ext.getBody().mask();
						var record=panel.getSelectionModel().getSelected();
						if(record ==null){
        					alert("请选择要修改的设备");
        					Ext.getBody().unmask();
        				}else{
        					var deviceId=record.get("deviceInfoID");
    						alert(deviceId);
    						Ext.Ajax.request({
    							url:'device_get',
    							params:{
    								deviceInfoID:deviceId			
    							},
    							success: function(response){
    								var result = Ext.util.JSON.decode(response.responseText);
    								if(result.success==true) {
    									showWindown("device_update");
    									deviceForm.getForm().setValues({
    										'deviceBaseInfo.deviceName':result.deviceName,
    										'deviceBaseInfo.deviceCode':result.deviceCode,
    										'deviceBaseInfo.deviceKKSCode':result.deviceKKSCode,
    										'deviceBaseInfo.devBuildPlace':result.devBuildPlace,
    										'deviceBaseInfo.devPlaceCode':result.devPlaceCode,
    										'deviceBaseInfo.devDrawingNo':result.devDrawingNo,
    										'deviceBaseInfo.devSpecification':result.devSpecification,
    										'deviceBaseInfo.deviceModel':result.deviceModel,
    										'deviceBaseInfo.devOutDate':result.devOutDate,
    										'deviceBaseInfo.devOutCode':result.devOutCode,
    										'deviceBaseInfo.devBuildCompany':result.devBuildCompany,
    										'deviceBaseInfo.devManuFactory':result.devManuFactory,
    										'deviceBaseInfo.devUseDepart':result.devUseDepart,
    										'deviceBaseInfo.devRunDate':result.devRunDate,
    										'deviceBaseInfo.devUseStatus':result.devUseStatus,
    										'deviceBaseInfo.devProvider':result.devProvider,
    										'deviceBaseInfo.devAddDate':result.devAddDate,
    										'deviceBaseInfo.devContractNo':result.devContractNo,
    										'deviceBaseInfo.addRemark':result.addRemark,
    										'deviceBaseInfo.deviceRemark':result.deviceRemark,
    										'deviceBaseInfo.deviceInfoID':result.deviceInfoID
    									});
    								}
    							}
    						});
        				}
						
					}
					}); 

				
			}
		},{
			text: '增加',disabled:true,id:'cToolInsertDeviceTreeBrotherNode',
			icon:'../image/public/user_add.gif',
        	handler:function(){
        		//alert("====");
        		Ext.getBody().mask();
        		showWindown("station_save");
        	//	alert("====sssssss");
        		store.reload();
        	}
		}],
		bbar:new Ext.PagingToolbar({
			pageSize : 10, //每页显示几条
			store : store,
			beforePageText:"当前第",   
            afterPageText:"页，共{0}页",   
            lastText:"尾页",   
            nextText :"下一页",   
            prevText :"上一页",   
            firstText :"首页",                           
	        displayInfo:true, 
	        displayMsg:'显示第{0}条数据到{1}条数据,一共有{2}条', 
	        emptyMsg:'没有记录'
		})
	});
	store.load({params:{start:0,limit:10}});//加载数据  注意，一旦使用了分页条， store.load();就必须在构造表格之后进行执行，否则将不起作用，并且将分页条和数据源关联，从而实现与表格共享数据模型

	
	
	new Ext.Viewport({  
		layout : 'border', // 使用border布局  
		border:false,
		items:[panel],
		listeners:{
			'beforerender' : function(cmp) {
				// set role
				var store=new Ext.data.JsonStore({
					url: '../system/role_loadFunc?funcCode=ControlDeviceTree',
					autoLoad : true,
					fields: ["functioncode"]
				});
				store.load({
				    callback: function(records, options, success){
				    	var i=0;
				    	for(; i<records.length; i++) {
				    		var id = records[i].get("functioncode");
				    		//alert('d'+Ext.getCmp('56').text);
				    		if(Ext.getCmp(id)!=null) {
				    			Ext.getCmp(id).setDisabled(false);
				    		}
				        }
				    },
				    scope: store
				});
			}
		}
	 }); 
	 
});
	</script>
</head>
<body>
<div id="form"></div>
<div id="grid"></div>
</body>
</html>